package com.ncp.model.biz.video;

import java.sql.*;
import java.util.*;

import com.ncp.model.dao.DbConnection;
import com.ncp.model.entity.news.NewsBean;
import com.ncp.model.entity.video.VideoBean;

public class VideoManager {
	private Connection ct=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
	private int pageCount=0;
	private int rowCount=0;
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getRowCount() {
		return rowCount;
	}
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}
	public ArrayList showNews(){
		ArrayList al=new ArrayList();
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="select * from VIDEO where picture!='null'";
			ps=ct.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				NewsBean ab=new NewsBean();
				ab.setCont(rs.getString(4));
				ab.setId(rs.getInt(1));
				ab.setTitle(rs.getString(2));
				//setPdate(rs.getTimestamp("pdate"));
				ab.setpDate(rs.getTimestamp(3));
				ab.setPicture(rs.getString(5));
				ab.setViews(rs.getInt(6));
				al.add(ab);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return al;
	}
	public static String[] split(String str, String sign) {
	  String[] strData = null;
	  StringTokenizer st1 = new StringTokenizer(str, sign);
	  strData = new String[st1.countTokens()];
	  int i = 0;
	  while (st1.hasMoreTokens()) {
	   strData[i] = st1.nextToken().trim();
	   i++;
	  }
		  return strData;
	}
	public boolean addVideo(String name,String writer,String descr,String picture,String realName ){
		boolean b=false;
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="insert into VIDEO values(null,'"+name+"','"+writer+"','"+descr+"','"+picture+"','"+realName+"',now(),0)";
			//System.out.println(sql);
			ps=ct.prepareStatement(sql);
			ps.executeUpdate();
			b=true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return b;
	}
	public ArrayList<VideoBean> getAllVideos(){
		ArrayList<VideoBean> al=new ArrayList<VideoBean>();
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="select * from VIDEO";
			ps=ct.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				VideoBean vb=new VideoBean();
				vb.setId(rs.getInt(1));
				vb.setName(rs.getString(2));
				vb.setWriter(rs.getString(3));
				vb.setDescr(rs.getString(4));
				vb.setPicture(rs.getString(5));
				vb.setRealName(rs.getString(6));
				vb.setpDate(rs.getTimestamp(7));
				vb.setViews(rs.getInt(8));
				al.add(vb);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return al;
	}
	public VideoBean getVideo(String id){
		VideoBean vb=new VideoBean();
		int views = 0;
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="select * from VIDEO where VideoID="+id;
			ps=ct.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				vb.setId(rs.getInt(1));
				vb.setName(rs.getString(2));
				vb.setWriter(rs.getString(3));
				vb.setDescr(rs.getString(4));
				vb.setPicture(rs.getString(5));
				vb.setRealName(rs.getString(6));
				vb.setpDate(rs.getTimestamp(7));
				vb.setViews(rs.getInt(8));
				views=rs.getInt(8);
				vb.setViews(views);
				this.viewsAdd(id, views);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return vb;
	}
	public void viewsAdd(String id,int views){
		try {
			ct = DbConnection.getConnByDataSource();
			views++;
			String sql="update VIDEO set views="+views+" where VideoID="+id;
			ps=ct.prepareStatement(sql);
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
	}
	public boolean delVideo(String id){
		Boolean b=false;
		try{
			ct = DbConnection.getConnByDataSource();
			String sql="delete from VIDEO where VideoID='"+id+"'";
			ps=ct.prepareStatement(sql);
			int num=ps.executeUpdate();
			if(num==1){
				b=true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(ps!=null){ps.close();}
				if(ct!=null){ct.close();}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return b;
	}
	public ArrayList<VideoBean> fenYe(int pageSize,int pageNow){
		ArrayList<VideoBean> al=new ArrayList<VideoBean>();
  		try {
  			ct = DbConnection.getConnByDataSource();
  			ps=ct.prepareStatement("select count(*) from VIDEO");
			rs=ps.executeQuery();
			if(rs.next()){
				rowCount=rs.getInt(1);	
			}
			if(rowCount%pageSize==0){
				pageCount=rowCount/pageSize;
			}else{
				pageCount=rowCount/pageSize+1;
			}
  			if(rs.next()){
  				rowCount=rs.getInt(1);
  			}
  			if(rowCount%pageSize==0){
  				pageCount=rowCount/pageSize;
  			}else{
  				pageCount=rowCount/pageSize+1;
  			}
  			ps=ct.prepareStatement("select * from VIDEO LIMIT "+(pageNow-1)*pageSize+",10");
  			rs=ps.executeQuery();
  			while(rs.next()){
  				VideoBean vb=new VideoBean();
				vb.setId(rs.getInt(1));
				vb.setName(rs.getString(2));
				vb.setWriter(rs.getString(3));
				vb.setDescr(rs.getString(4));
				vb.setPicture(rs.getString(5));
				vb.setRealName(rs.getString(6));
				vb.setpDate(rs.getTimestamp(7));
				vb.setViews(rs.getInt(8));
				al.add(vb);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return al;
	}
	public void closeResource(){
		try {
			if(rs!=null){
				rs.close();
				rs=null;
			}
			if(ps!=null){
				ps.close();
				ps=null;
			}
			if(ct!=null){
				ct.close();
				ct=null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
